package com.lagou.edu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;

/**
 * 邮件控制器
 */
@RestController
@RequestMapping({"/email",""})
@RefreshScope
public class EmailController {

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    String username;


    /**
     * 进行发送邮件
     * @return
     */
    @GetMapping("{email}/{code}")
    public boolean send(@PathVariable("email") String email,@PathVariable("code") String code) throws MessagingException {
        try {
            //简单邮件
            SimpleMailMessage simpleMailMessage =new SimpleMailMessage();
            simpleMailMessage.setFrom(username);
            simpleMailMessage.setTo(email);
            simpleMailMessage.setSubject("注册验证码");
            simpleMailMessage.setText("验证码为:"+code);
            mailSender.send(simpleMailMessage);

            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

}
